<template>
	<view class="loginPage">
		<button @getuserinfo="wxLogin"
			:style="{backgroundImage: 'linear-gradient(to right, #bb1e1e, #d25939)',border:'none',color:'#fff'}"
			class="login_button login_button_wx" open-type="getUserInfo">
			<u-icon name="weixin-fill" :size="50"></u-icon>
			授权登录
		</button>
		<u-checkbox-group>
			<u-checkbox v-model="value" active-color="#cb2921" name="true">
				我已阅读并同意隐私政策与用户协议
			</u-checkbox>
		</u-checkbox-group>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'

	import {
		wxloginByCode
	} from "@api"


	export default {
		name:"authorization",
		data() {
			return {
				value: "true"
			}
		},
		methods: {
			...mapMutations("USER", ['setUserInfo', 'setToken']),
			wxLogin({
				detail: {
					userInfo
				}
			}) {
				let that = this;
				uni.login({
					provider: 'weixin',
					success({
						code
					}) {
						console.log("code:  ", code);
						console.log("userInfo:", userInfo);
						userInfo["code"] = code;
						wxloginByCode(userInfo).then(res => {
							console.log(res);
							that.setUserInfo(res.user);
							that.setToken(res.token);
							if (res.token == 1) {
								that.$Router.push({
									name: 'identity'
								});
							} else {
								uni.reLaunch({
									url: '/pages/home/home'
								});
							}
						})
					},
					fail(err) {
						uni.showToast({
							title: "登录失败",
							content: "原因：" + JSON.stringify(err)
						})
					},
					complete() {
						uni.hideLoading()
					}
				})
			}
		},
		onLoad() {

		}
	}
</script>

<style lang="scss" scoped>
	.loginPage {
		padding: 40rpx;
		margin-top: 300rpx;
		display: flex;
		flex-wrap: wrap;
		justify-content: center;
	}

	.login {
		&_button {
			width: 100%;
			height: 80rpx;
			border-radius: 40rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 32rpx;

			&:after {
				border: none;
			}
		}
	}
</style>

 
 
 